package com.zhaodm.practise.client;

import com.zhaodm.practise.service.ICat;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	private ICat catService;
	private Button get;
	EditText color;
	EditText weight;
	
	private ServiceConnection conn = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			catService = ICat.Stub.asInterface(service);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			catService = null;
		}
		
	};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        get = (Button)findViewById(R.id.getServiceStatus);
        color = (EditText)findViewById(R.id.color);
        weight = (EditText)findViewById(R.id.weight);
        
        Intent intent = new Intent();
        intent.setAction("com.zhaodm.practise.service.AIDL_SERVICE");
        
        bindService(intent, conn, Service.BIND_AUTO_CREATE);
        get.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					color.setText(catService.getColor());
					weight.setText(catService.getWeight()+"");
				} catch (RemoteException e) {
					e.printStackTrace();
				}
			}
        	
        });
    }
    
    /* (non-Javadoc)
     * @see android.app.Activity#onDestroy()
     */
    public void onDestroy() {
    	super.onDestroy();
    	this.unbindService(conn);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
